package books.borrowing.dao.impl;

/**
 * ANCIENNE METHODE
 */

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import books.borrowing.dao.ISubscriberDao;
import books.borrowing.entity.SubscriberEntity;

@Repository("subscriberDao")
public class SubscriberDao implements ISubscriberDao {

	private static HibernateTemplate _template;

	public void setSessionFactory(SessionFactory sessionFactory) {
		_template = new HibernateTemplate(sessionFactory);
	}
	
	public void save(SubscriberEntity _subscriber) {
		_template.save(_subscriber);
	}

	public void update(SubscriberEntity _subscriber) {
		_template.update(_subscriber);
	}

	public void delete(SubscriberEntity _subscriber) {
		_template.delete(_subscriber);
	}

	@SuppressWarnings("unchecked")
	public SubscriberEntity connect(String _login, String _password)
			throws Exception {

		String[] params = { _login, _password };

		List<SubscriberEntity> subscribers = _template.find(
				"from SubscriberEntity where login = ? and password = ?",
				params);
		if (subscribers == null)
			throw new Exception("Identifiant ou Mot de passe Invalid");
		return subscribers.get(0);
	}

	@SuppressWarnings("unchecked")
	public SubscriberEntity findSubscriberByLogin(String _login)
			throws Exception {
		List<SubscriberEntity> subscribers = _template.find(
				"from SubscriberEntity where login=?", _login);
		if (subscribers == null)
			throw new Exception("Identifiant inexistant !");
		return subscribers.get(0);
	}

}
